<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="停车场" prop="parkingLotCode" v-hasPermi="['parking:recordCharge:edit']">
        <el-select v-model="queryParams.parkingLotCode" placeholder="请选择停车场" clearable>
          <el-option
            v-for="dict in parkingInfoList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="车牌号码" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="serialNumber">
        <el-input
          v-model="queryParams.serialNumber"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进场时间">
        <el-date-picker
          v-model="daterangeCarInTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="出场时间">
        <el-date-picker
          v-model="daterangeCarOutTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          :disabled="true"-->
<!--          v-hasPermi="['parking:recordCharge:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['parking:recordCharge:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['parking:recordCharge:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['parking:recordCharge:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table max-height="500" v-loading="loading" :data="recordChargeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键ID" width="80" align="center" prop="id" />-->
      <el-table-column label="停车场名称" align="center" prop="parkingLotName" />
      <el-table-column label="车牌号码" width="100" align="center" prop="carNum" />
      <el-table-column label="进场时间" align="center" prop="carInTime" width="180"/>
      <el-table-column label="出场时间" align="center" prop="carOutTime" width="180"/>
      <el-table-column label="停车时长" align="center" prop="parkTime" />
      <el-table-column label="消费金额" align="center" prop="consumeMoney" />
      <el-table-column label="实收款金额" align="center" prop="feeAmount" />
      <el-table-column label="优惠金额" align="center" prop="couponAmount" />
      <el-table-column label="订单号" width="200" align="center" prop="serialNumber" />
      <el-table-column label="操作" width="176" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-guide"
            @click="handleOpenGate(scope.row)"
            v-hasPermi="['parking:recordCharge:list']"
          >远程开闸</el-button>-->
          <el-button
            type="text"
            size="small"
            icon="el-icon-view"
            @click="handlePreview(scope.row)"
            v-hasPermi="['parking:recordCharge:list']"
          >查看</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['parking:recordCharge:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['parking:recordCharge:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆收费记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="联网平台ID" prop="platformId">
          <el-input v-model="form.platformId" placeholder="请输入联网平台ID"/>
        </el-form-item>
        <el-form-item label="交易订单ID" prop="uniqueId">
          <el-input v-model="form.uniqueId" placeholder="请输入下级平台交易订单ID" />
        </el-form-item>
        <el-form-item label="停车记录ID" prop="recordId">
          <el-input v-model="form.recordId" placeholder="请输入停车场停车记录ID" />
        </el-form-item>
        <el-form-item label="停车场名称" prop="parkingLotName">
          <el-input v-model="form.parkingLotName" placeholder="请输入停车场名称" />
        </el-form-item>
        <el-form-item label="停车场编码" prop="parkingLotCode">
          <el-input v-model="form.parkingLotCode" placeholder="请输入停车场编码" />
        </el-form-item>
        <el-form-item label="订单号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="进场时间" prop="carInTime">
          <el-date-picker clearable
            v-model="form.carInTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择进场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出场时间" prop="carOutTime">
          <el-date-picker clearable
            v-model="form.carOutTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择出场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="停车时长" prop="parkTime">
          <el-input v-model="form.parkTime" placeholder="请输入停车时长" />
        </el-form-item>
        <el-form-item label="车牌号码" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号码" />
        </el-form-item>
        <el-form-item label="消费金额" prop="consumeMoney">
          <el-input placeholder="请输入消费金额" v-model="form.consumeMoney">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="实收款金额" prop="feeAmount">
          <el-input placeholder="请输入实收款金额" v-model="form.feeAmount">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="优惠金额" prop="couponAmount">
          <el-input placeholder="请输入优惠金额" v-model="form.couponAmount">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="支付时间" prop="chargeTime">
          <el-input v-model="form.chargeTime" placeholder="请输入支付时间" />
        </el-form-item>
        <el-form-item label="车主名称" prop="ownerName">
          <el-input v-model="form.ownerName" placeholder="请输入车主名称" />
        </el-form-item>
        <el-form-item label="车主电话" prop="ownerPhone">
          <el-input v-model="form.ownerPhone" placeholder="请输入车主电话" />
        </el-form-item>
        <el-form-item label="收费员名称" prop="operatorName">
          <el-input v-model="form.operatorName" placeholder="请输入收费员名称" />
        </el-form-item>
        <el-form-item label="优惠券码" prop="discountCode">
          <el-input v-model="form.discountCode" placeholder="请输入优惠券码" />
        </el-form-item>
        <el-form-item label="优惠券批次号" prop="batchNumber">
          <el-input v-model="form.batchNumber" placeholder="请输入优惠券批次号" />
        </el-form-item>
        <el-form-item label="开发票额" prop="invoiceAmount">
          <el-input v-model="form.invoiceAmount" placeholder="请输入开发票额" />
        </el-form-item>
        <el-form-item label="税额" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="请输入税额" />
        </el-form-item>
        <el-form-item label="税率" prop="taxRate">
          <el-input v-model="form.taxRate" placeholder="请输入税率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-hasPermi="['parking:recordCharge:edit']">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRecordCharge, getRecordCharge, delRecordCharge, addRecordCharge, updateRecordCharge, openGate } from "@/api/parking/recordCharge";
import { selectParkingInfoList } from "@/api/parking/selectList";

export default {
  name: "RecordCharge",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆收费记录表格数据
      recordChargeList: [],
      parkingInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      daterangeCarInTime: [],
      // 税率时间范围
      daterangeCarOutTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parkingLotName: null,
        serialNumber: null,
        carInTime: null,
        carOutTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getParkingInfoList();
  },
  methods: {
    /** 查询车辆收费记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCarInTime && '' != this.daterangeCarInTime) {
        this.queryParams.params["beginCarInTime"] = this.daterangeCarInTime[0];
        this.queryParams.params["endCarInTime"] = this.daterangeCarInTime[1];
      }
      if (null != this.daterangeCarOutTime && '' != this.daterangeCarOutTime) {
        this.queryParams.params["beginCarOutTime"] = this.daterangeCarOutTime[0];
        this.queryParams.params["endCarOutTime"] = this.daterangeCarOutTime[1];
      }
      listRecordCharge(this.queryParams).then(response => {
        this.recordChargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkingInfoList() {
      selectParkingInfoList().then(response => {
        this.parkingInfoList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        platformId: null,
        uniqueId: null,
        recordId: null,
        parkingLotName: null,
        parkingLotCode: null,
        serialNumber: null,
        carInTime: null,
        carOutTime: null,
        parkTime: null,
        carNum: null,
        consumeMoney: null,
        feeAmount: null,
        couponAmount: null,
        feeType: null,
        chargeTime: null,
        ownerName: null,
        ownerPhone: null,
        operatorName: null,
        favorableType: null,
        discountCode: null,
        batchNumber: null,
        invoiceAmount: null,
        taxNumber: null,
        taxRate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.carInTime){
        this.queryParams.carInTime = this.queryParams.carInTime + ' 00:00:00';
      }
      if(this.queryParams.carOutTime){
        this.queryParams.carOutTime = this.queryParams.carOutTime + ' 00:00:00';
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCarInTime = [];
      this.daterangeCarOutTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handlePreview(row) {
      this.reset();
      const id = row.id || this.ids
      getRecordCharge(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "车辆收费记录";
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆收费记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecordCharge(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆收费记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecordCharge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecordCharge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆收费记录编号为"' + ids + '"的数据项？').then(function() {
        return delRecordCharge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('parking/recordCharge/export', {
        ...this.queryParams
      }, `recordCharge_${new Date().getTime()}.xlsx`)
    },
    /** 远程开闸操作 */
    handleOpenGate(row) {
      this.$modal.confirm('是否确认为 '+row.carNum+' 远程开闸？').then(function() {
        return openGate(row.serialNumber);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("远程开闸成功");
      }).catch(() => {});
    },
  }
};
</script>
